function [rho,bet,sigma,res]=ar_adf(y,nlags); 
% =======================================
% [bet,sigma,res]=ar_adf(y,nlags); 
% 
% Used in PRIOR_STDSIM.m 
%
% Compute AR ADF type regression 
% y(t) = B(1)*y(t-1) + B(2)*d( y(t-1) ) +....
%        B(nlags+1)*d( y(t-nlags) ) + B(end)*1
% Note that first observation used is at 
% NLAGS + 2 
% e.g. if date( y(1) ) = 1954q3 
% first date is 1955q2 
% Alejandro Justiniano 
% 
% RHO     B(1) 
% BETA    Full coefficient vector where B(1) is AR 
%         B(END) = constant 
% SIGMA   E'*E / ( T - NK ) 
% RES     Residual 
% =======================================
ydif=y(2:end)-y(1:end-1);
yd=lagmat([0;ydif],nlags);
yl=lagmat(y,2);
cut=max(nlags+2,2); 
yl=yl(cut:end,:); yd=yd(cut:end,:); 
T=length(yl); 
x=[yl(:,2) yd(:,2:end) ones(T,1)];
k=size(x,2); 
bet=((x'*x)\eye(k))*(x'*yl(:,1));
res=yl(:,1)-x*bet(:);
sigma=res'*res/( T - k); 
rho=bet(1); 